import { Base } from '../../utils/base.js'

class Order extends Base {

  constructor() {
    super();
  }

  /*获得结算数据*/
  getOrdersToPay(params,callback) {
    var allParams = {
      url: 'cart/confirm',
      data: params,
      type: 'Post',
      sCallback: function (data) {
        callback && callback(data);  
      }
    };
    this.request(allParams);
  }

  // 获取订单详情
  getOrderDetails(param,callback){
    var allParams = {
      url: 'seller/order-info' + '?expand=orderSku,shop',
      data: param,
      type: 'Post',
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(allParams);
  }
  //获取订单退款信息
  getRefundInfo(param, callback) {
    var allParams = {
      url: 'order/refuse-info',
      data: param,
      type: 'Post',
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(allParams);
  }
  // 获取订单支付信息
  getOrderPay(param, callback) {
    var allParams = {
      url: 'order/pay',
      data: param,
      type: 'Post',
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(allParams);
  }
  /*下订单*/
  doOrder(param, callback) {
    var that = this;
    var allParams = {
      url: 'cart/checkout',
      data: param,
      type: 'post',
      sCallback: function (data) {
        callback && callback(data);
      },
      eCallback: function () {
      }
    };
    this.request(allParams);
  }
  //小程序微信支付
  execPay(params, callback) {
    var allParams = {
      url: 'pay/xcx',
      type: 'post',
      data: params,
      sCallback: function (data) {
        if(data.is_cash==1){
          var data = data.items;
          var timeStamp = data.timeStamp;
          if (timeStamp) { //可以支付
            wx.requestPayment({
              'timeStamp': timeStamp.toString(),
              'nonceStr': data.nonceStr,
              'package': data.package,
              'signType': data.signType,
              'paySign': data.paySign,
              success: function () {
                callback && callback(2);
              },
              fail: function () {
                callback && callback(1);
              }
            });
          } else {
            callback && callback(0);
          }
        } else{
          callback && callback(data);
        }

      }
    };
    this.request(allParams);
  }
  //余额支付
  accountPay(params, callback) {
    var allParams = {
      url: 'pay/money',
      data: params,
      type: 'post',
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(allParams);
  }
  //商家订单列表
  lists(param, cb) {
    var that = this;
    var allParams = {
      url: 'seller/order?page=' + param.page +'&expand=orderSku,shop',
      data: param,
      type: 'post',
      sCallback: function (res) {
        cb && cb(res)
      }
    };
    this.request(allParams);
  }
  //获取直接购买get过来的sku_id和num 获得结算数据*/
  getbuy(params,callback) {
    var allParams = {
      url: 'cart/getbuy',
      data: params,
      type: 'Post',
      sCallback: function (data) {
        var status = data.status
        if(status==1){
          callback && callback(data);
        }else if(status==-1){
          wx.showToast({
            title: '该活动规格已下架',
            icon: 'none',
            duration: 2000
          })

        }else if(status==-2){
          wx.showToast({
            title: '该活动已结束',
            icon: 'none',
            duration: 2000
          })

        }else if(status==-3){
          wx.showToast({
            title: '大于库存',
            icon: 'none',
            duration: 2000
          })
          
        } else {
          wx.showToast({
            title: '操作失败~',
            icon: 'none',
            duration: 2000
          })
        }

      }
    };
    this.request(allParams);
  }

  /*取消订单*/
  cancelOrder(id,callback) {
    var allParams = {
      url: 'order/cancel' ,
      data: {order_id:id},
      type: 'post',
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(allParams);
  }
  /*系统关闭订单*/
  scancelOrder(id, callback) {
    var allParams = {
      url: 'order/scancel',
      data: { order_id: id },
      type: 'post',
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(allParams);
  }
  /*删除订单*/
  deleteOrder(id, callback) {
    var allParams = {
      url: 'order/delete',
      data: { order_id: id },
      type: 'post',
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(allParams);
  }

  /*申请退款。收货前*/
  refundOrder(params, callback) {
    var allParams = {
      url: 'order/apply-refund',
      data: params,
      type: 'post',
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(allParams);
  }
  // 再次购买，跳转到购物车
  buyAgain(id){
    wx.switchTab({
      url: '../cart/cart?id='+id,
    })
  }
  
  //确认收货
  confirmOrder(id, callback){
    var allParams = {
      url: 'order/confirm',
      data: { order_id: id },
      type: 'post',
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(allParams);
  }
  
}

export { Order };